using Volo.Abp.Authorization.Permissions;
using Volo.Abp.UI.Navigation;

using WL.Think.Abp.Localization;
using WL.Think.Abp.OpenIddict.Permission;

namespace WL.Think.Abp.OpenIddict.Blazor.Menu
{
    public class ThinkAbpOpenIddictMenuContributor : IMenuContributor
    {
        public Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if(context.Menu.Name == StandardMenus.Main)
            {
                InitOpenIddictMenuAsync(context);
            }
            return Task.CompletedTask;
        }

        private Task InitOpenIddictMenuAsync(MenuConfigurationContext context)
        {
            var l = context.GetLocalizer<ThinkResource>();
            var adminMenu = context.Menu.GetAdministration();

            var menu = new ApplicationMenuItem(
                OpenIddictMenuNames.GroupName,
                l["OpenIddictManagement"],
                icon: "fa fa-shield"
            );

            menu.AddItem(new ApplicationMenuItem(
                OpenIddictMenuNames.Applications,
                l["Applications"],
                url: "~/openiddict/applications",
                icon: "fa fa-file"
            )).RequirePermissions(OpenIddictPermissions.Applications.Default);

            menu.AddItem(new ApplicationMenuItem(
                OpenIddictMenuNames.Scopes,
                l["Scopes"],
                url: "~/openiddict/scopes",
                icon: "fa fa-file"
            )).RequirePermissions(OpenIddictPermissions.Scopes.Default);


            adminMenu.AddItem(menu);

            return Task.CompletedTask;
        }
    }
}
